<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>wangEditor demo</title>
    <link id="easyuiTheme" rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.5.1/themes/gray/easyui.css" />
    <link id="easyuiTheme" rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.5.1/themes/icon.css" />
    <script type="text/javascript" src="/static/js/jquery-1.10.1.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.5.1/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/static/js/HhUtil.js?t=20170109"></script>
    <!-- 注意， 只需要引用 JS，无需引用任何 CSS ！！！-->
    <script type="text/javascript" src="/static/wangeditor/wangEditor.min.js"></script>
    <!-- [扩展样式] -->
    <link rel="stylesheet" type="text/css" href="/static/css/dreamlu.css" />
    <link rel="stylesheet" type="text/css" href="/static/css/icommon.css" />
    <link rel="stylesheet" type="text/css" href="/static/css/login.css" />


</head>
<body>
    <a href="javascript:void(0);" class="easyui-linkbutton"
                           data-options="iconCls:'icon-back',plain:true" onclick="cancelFun();">返回</a>

    <h4>广告图片</h4>
    <input type="hidden" id="title_url" name="title_url" >
    <img id="title_img" src="/static/aui/image/demo1.png" style="height: 80px;width: 50%;float: left">
    <div  style="margin-bottom:20px">
        <form id="upload_img" enctype="multipart/form-data" method="post">
        <input class="easyui-filebox" id="file" name="file" style="width:100%">
        </form>
        <a style="float: right" href="javascript:saveFun()" class="user-easyui-linkbutton-save" data-options="plain:true,iconCls:'icon-save'"></a>
    </div>
    <table>
        <th>跳转类型:</th>
        <td><select id="type" class="easyui-combobox" name="type" style="width: 120px">
        <option value="0">编辑内容</option>
        <option value="1">H5页面</option>
        <option value="2">原生页面</option>
    </select></td>
      </table>

    <h4>URL地址</h4>
    <input class="easyui-textbox" id="url" style="width:70%;height:28px">
    <h4>标题</h4>
    <input class="easyui-textbox" id="title" style="width:70%;height:28px">
    <h4 style="clear: both">详情</h4>
    <div id="editor" >
    </div>
    <div style="text-align: center">
        <a href="javascript:void(0);" class="easyui-linkbutton"
                           data-options="iconCls:'icon-save',plain:true" onclick="submitFun();">提交</a>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <a href="javascript:void(0);" class="easyui-linkbutton"
                            data-options="iconCls:'icon-cancel',plain:true" onclick="cancelFun();">取消</a>
    </div>


    <script type="text/javascript">
        $('.user-easyui-linkbutton-save').linkbutton({text: '保存', plain: true, iconCls: 'icon-save'});

        function saveFun() {
            $("#upload_img").form("submit", {
                url: "/upadimg",
                success: function (data) {
                    data = JSON.parse(data);
                    if (data.code == "0") {
                        $("#title_img").attr("src", data.url)
                        $("#title_url").val(data.url)
                        HdUtil.messager.slide("上传成功");
                    } else {
                        HdUtil.messager.error("上传失败");
                    }
                }
            });
        }

        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(r[2]); return null; //返回参数值
        }
        var type = getUrlParam('type');

        var E = window.wangEditor
        var editor = new E('#editor')
        // 或者 var editor = new E( document.getElementById('#editor') )
        editor.customConfig.uploadImgServer = '/upnewsimg'
        editor.customConfig.uploadFileName = 'file'
        editor.customConfig.menus = [
                                        'head',  // 标题
                                        'bold',  // 粗体
                                        'italic',  // 斜体
                                        'underline',  // 下划线
                                        'strikeThrough',  // 删除线
                                        'foreColor',  // 文字颜色
                                        'backColor',  // 背景颜色
                                        'link',  // 插入链接
                                        'list',  // 列表
                                        'justify',  // 对齐方式
                                        'quote',  // 引用
                                        'image',  // 插入图片
                                        'table',  // 表格
                                        'code',  // 插入代码
                                        'undo',  // 撤销
                                        'redo'  // 重复
                                    ]
        editor.create();

        function submitFun() {
            // 读取 html
            var title_url=$('#title_url').val();
            var title=$('#title').val();
            var url=$('#url').val();
            var type=$("#type").combobox('getValue');
            var content=editor.txt.html();
            $.ajax({
                type: "POST",
                url: "/addad",
                dataType:'json',
                data: {title_url: title_url, title: title, content: content, type: type, url: url},//也可以是字符串链接"name=张三&sex=1"，建议用对象
                success: function(data){
                    //实际操作的时候，返回的json对象中可能会有成功错误的判断标记，所以可能也需要判断一下
                    if (data.code=="0" ){
                        location.href = "/static/ad/ad_list.html"
                    }else{
                        alert(data.msg);
                    }
                }
            });
        }

        function cancelFun() {
            // 读取 text
            location.href = "/static/ad/ad_list.html"
        }
    </script>
</body>
</html>